//
//  DBJDoctorDetailViewController.m
//  DBJ
//
//  Created by 简楚君 on 2016/11/13.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "DBJDoctorDetailViewController.h"
#import <QuartzCore/QuartzCore.h>
#import <Masonry.h>
#import <UIImageView+WebCache.h>

@interface DBJDoctorDetailViewController ()<UIScrollViewDelegate>

@property(strong,nonatomic)UIView *indicator;

@property(strong,nonatomic)NSMutableArray<UIView *> *viewArrs;
/**
 头像图片
 */
@property (weak, nonatomic) IBOutlet UIImageView *iconView;

/**
 锦旗
 */
@property (weak, nonatomic) IBOutlet UIButton *accuracy;

/**
 姓名
 */
@property (weak, nonatomic) IBOutlet UILabel *name;

/**
 职位
 */
@property (weak, nonatomic) IBOutlet UILabel *position;

/**
 工作地点
 */
@property (weak, nonatomic) IBOutlet UILabel *workPlace;

/**
 左边label
 */
@property (weak, nonatomic) IBOutlet UILabel *leftLabel;

/**
 右边label
 */
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;

/**
 下方滚动视图
 */
@property (weak, nonatomic) IBOutlet UIScrollView *scrollContentView;

@property (weak, nonatomic) IBOutlet UIView *labelbgView;
@property(weak,nonatomic)UILabel *codition;
@property(weak,nonatomic)UILabel *introduce;
@property(assign,nonatomic)BOOL isSelect;
@end

@implementation DBJDoctorDetailViewController

-(instancetype)init{
    if (self = [super init]) {
        UIStoryboard *story = [UIStoryboard storyboardWithName:@"DBJDoctorDetailViewController" bundle:nil];
        self = [story instantiateInitialViewController];
    }
        return self;
}
-(void)dbjSetupNavigationItems{
    [super dbjSetupNavigationItems];
    //设置nav名称
    self.title = @"医生详情";
}

-(void)dbjSetupSubviews{
    [super dbjSetupSubviews];
    
    self.isSelect = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(clickNavRightBarBtn:)];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.labelbgView.backgroundColor = DBJColor;
    self.viewArrs = [NSMutableArray arrayWithCapacity:2];
    self.scrollContentView.delegate = self;
    
        /*------------* 设置头像图片成圆图 *-------------*/
    self.iconView.layer.masksToBounds = YES;
    self.iconView.layer.cornerRadius = self.iconView.bounds.size.width*0.5;
    self.iconView.layer.borderWidth = 3;
    self.iconView.layer.borderColor = [UIColor whiteColor].CGColor;
    
    /*------------* 设置中间滚动条 *-------------*/
    UIView *v = [[UIView alloc]init];
    v.backgroundColor = DBJColor;
    [self.view addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.leftLabel.mas_bottom);
        make.height.mas_equalTo(3);
        make.width.equalTo(self.leftLabel);
    }];
    self.indicator = v;

    /*------------* 设置label *-------------*/
    self.leftLabel.textColor = DBJColor;
    self.leftLabel.tag = 1;
    self.leftLabel.userInteractionEnabled = YES;
    
    self.rightLabel.textColor =DBJColor;
    self.rightLabel.tag = 2;
    self.rightLabel.userInteractionEnabled = YES;
    
        /*------------* 创建手势 *-------------*/
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.leftLabel addGestureRecognizer:tapLeft];
    
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.rightLabel addGestureRecognizer:tapRight];
    
        /*------------* 设置scrollview相关属性 *-------------*/
    self.scrollContentView.showsVerticalScrollIndicator = NO;
    self.scrollContentView.showsHorizontalScrollIndicator= NO;
    self.scrollContentView.pagingEnabled = YES;
    self.scrollContentView.bounces = NO;
    [self.scrollContentView setBackgroundColor:[UIColor whiteColor]];
    
    //设置底部视图
    [self setFooterUI];
}

-(void)clickNavRightBarBtn:(UIBarButtonItem *)sender{
    if (!self.isSelect) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplate"]];
        [DBJTools dbjShowSVSuccessTitle:@"成功关注" andTime:0.8];
    }else{
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplateEmpty"]];
        [DBJTools dbjShowSVSuccessTitle:@"取消关注" andTime:0.8];
    }
    self.isSelect = ! self.isSelect;
}


#pragma mark  - setter方法

-(void)setDetailModel:(DBJDocListModel *)detailModel{
    _detailModel = detailModel;
    self.name.text = _detailModel.doctor_name;
    self.position.text = _detailModel.doctor_title_name;
    //self.workPlace.text = _detailModel.doctor_hospital_name;
    if (detailModel.hospital_name) {
        self.workPlace.text = detailModel.hospital_name;
    }else{
        self.workPlace.text = detailModel.doctor_hospital_name;
    }
    [self.accuracy setTitle:_detailModel.accuracy forState:UIControlStateNormal];
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:_detailModel.doctor_portrait] placeholderImage:[UIImage imageNamed:@"doctor_defaultphoto_male"]];
}

-(void)setConditionModel:(DBJCureConditionModel *)conditionModel{
    NSMutableString *str = [NSMutableString string];
    for (NSString *s in conditionModel.receiving_settings) {
        [str appendFormat:@"\n\t%@\n",s];
    }
    self.codition.text = [NSString stringWithFormat:@"\t%@%@",conditionModel.receiving_setting_extra,str];
}

-(void)setIntroduceModel:(DBJDocIntrodunceModel *)introduceModel{
    self.introduce.text = introduceModel.introduction;
}
#pragma mark - 手势相关

-(void)clickLabel:(UITapGestureRecognizer *)sender{
    if (sender.view.tag == 1) {
        [self.scrollContentView setContentOffset:CGPointMake(0, 0) animated:YES];
        // 开始动画
        [UIView beginAnimations:nil context:nil];
         // 动画时长
        [UIView setAnimationDuration:2.0];
        [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view);
            make.top.equalTo(self.leftLabel.mas_bottom);
        }];
        // 提交动画
        [UIView commitAnimations];
    }
    else if(sender.view.tag == 2){
        [self.scrollContentView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
            [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/2);
                make.top.equalTo(self.rightLabel.mas_bottom);
            }];
        }];
    }
}

#pragma mark - 设置底部视图
/**
 设置底部视图
 */
-(void)setFooterUI{
    //定义一个宽
    CGFloat width = self.view.bounds.size.width;
    //定义一个高
    CGFloat height = self.view.bounds.size.height;
    //定义个数
    int index = 2;
    for (int i = 0; i < index; i++) {
        UIView *footView = [[UIView alloc]init];
        //背景色
        footView.backgroundColor = [UIColor whiteColor];
        [self.scrollContentView addSubview:footView];
        [self.viewArrs addObject:footView];
        footView.frame = CGRectMake(width*i, 0, width, height);
    }
    //设置偏移量
    self.scrollContentView.contentSize = CGSizeMake(width*index, 0);
    //设置接诊条件和医生简介(label)
    [self setLabel];
}


/**
 设置底部视图中的文字显示
 */
-(void)setLabel{
    UILabel *condition = [[UILabel alloc]init];
    [self.viewArrs[0] addSubview:condition];
    condition.textAlignment = NSTextAlignmentLeft;
    [condition mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.viewArrs[0]);
    }];
    //自动折行设置
    condition.lineBreakMode = NSLineBreakByWordWrapping;
    condition.numberOfLines = 0;
    self.codition = condition;
    
    UILabel *intro = [[UILabel alloc]init];
    [self.viewArrs[1] addSubview:intro];
    intro.textAlignment = NSTextAlignmentLeft;
    [intro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.viewArrs[1]);
    }];
    //自动折行设置
    intro.lineBreakMode = NSLineBreakByWordWrapping;
    intro.numberOfLines = 0;
    self.introduce = intro;
}

#pragma mark - scrollview代理方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat width = self.view.bounds.size.width;
    NSInteger index = self.scrollContentView.contentOffset.x/width;
    if (index == 0) {
        [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view);
                make.top.equalTo(self.leftLabel.mas_bottom);
            }];
        }];
    }else if (index == 1){
        [UIView animateWithDuration:0.6 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/2);
                make.top.equalTo(self.rightLabel.mas_bottom);
            }];
        }];
           }
}

@end
